//
//  LNSlideView.m
//  LNSlideView
//
//  Created by 申铭 on 15/11/15.
//  Copyright © 2015年 shenming. All rights reserved.
//

#import "LNSlideView.h"

#define kBtnDefaultCount 4 // 默认顶部显示按钮的个数
#define kTopSpace 64
#define kSlideIndicatorViewHeight 4 // 指示View的高度

@interface LNSlideView()<UIScrollViewDelegate>
{
    int _topHeight;
    CGRect _myViewFrame; // 视图的大小
    NSInteger _viewCount; // 视图数量
    UIView *_topMainView;
    UIScrollView *_scrollView; // 下边滚动的View
    UIScrollView *_topMainScrollView; // 顶部按钮滚动view
    NSMutableArray *_controlArray; // 底部table数组
    NSMutableArray *_topTitleArray;
    NSMutableArray *_topViews; // 顶部按钮数组
    UIView *_slideIndicatorView; // 指示View
    NSMutableArray *_topBtns; // 顶部的btn 用来设置颜色
}
@end

@implementation LNSlideView

- (instancetype)initWithFrame:(CGRect)frame controlArray:(NSMutableArray *)controlArray topTitleArray:(NSMutableArray *)topTitleArray topViewHeight:(int)topViewHeight
{
    if (self = [super initWithFrame:frame]) {
        _topHeight = topViewHeight;
        _myViewFrame = frame;
        _viewCount = controlArray.count;
        _topTitleArray = topTitleArray;
        _controlArray = controlArray;
        
        [self initScrollView];
        [self initTopBtn];
        [self initDownTables];
        [self initSlideIndicatorView];
    }
    return self;
}

- (void)initScrollView
{
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, _myViewFrame.size.width, _myViewFrame.size.height - kTopSpace)];
    _scrollView.backgroundColor = [UIColor greenColor];
    _scrollView.contentSize = CGSizeMake(_myViewFrame.size.width * _viewCount, _myViewFrame.size.height - kTopSpace);
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.delegate = self;
    
    [self addSubview:_scrollView];
}

- (void)initTopBtn
{
    CGFloat width = _myViewFrame.size.width / kBtnDefaultCount;
    
    // 最多现实4个btn 多余的需要滚动
    if(_viewCount <= kBtnDefaultCount){
        width = _myViewFrame.size.width / _viewCount;
    }
    
    _topMainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _myViewFrame.size.width, _topHeight)];
    _topMainScrollView = [[UIScrollView alloc]initWithFrame:_topMainView.frame];
    _topMainScrollView.showsHorizontalScrollIndicator = NO;
    _topMainScrollView.showsVerticalScrollIndicator = YES;
    _topMainScrollView.bounces = YES;
    _topMainScrollView.delegate = self;
    
    _topMainScrollView.backgroundColor = [UIColor yellowColor];
    
    if (_viewCount <= kBtnDefaultCount) {
        _topMainScrollView.contentSize = CGSizeMake(_myViewFrame.size.width, _topHeight);
    } else {
        _topMainScrollView.contentSize = CGSizeMake(width * _viewCount, _topHeight);
    }
    
    [_topMainView addSubview:_topMainScrollView];
    [self addSubview:_topMainView];
    
    _topBtns = [NSMutableArray array];
    for (int i = 0; i < _viewCount; i++) {
        NSString *title = _topTitleArray[i];
        
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(i * width, 0, width, _topHeight)];
        btn.tag = i;
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        [_topBtns addObject:btn];
        [_topMainScrollView addSubview:btn];
    }
}

- (void)clickBtn:(UIButton *)btn
{
    if ([_delegate respondsToSelector:@selector(lnSlideView:currentPage:)]) {
        [_delegate lnSlideView:self currentPage:(int)btn.tag];
    }
    [self updateTopMainScorllViewPosition:(int)btn.tag];
    [_scrollView setContentOffset:CGPointMake(btn.tag * _myViewFrame.size.width, 0) animated:YES];
}

- (void)initDownTables
{
    for (int i = 0 ; i < _controlArray.count; i ++) {
        UITableViewController *table = _controlArray[i];
        table.view.frame = CGRectMake(i * _myViewFrame.size.width, _topHeight, _myViewFrame.size.width, _scrollView.frame.size.height - _topHeight);
        
        [_scrollView addSubview:table.view];
    }
}

#pragma mark 初始化滑动的指示View
- (void)initSlideIndicatorView
{
    CGFloat width = _myViewFrame.size.width / kBtnDefaultCount;
    
    if(_viewCount <= kBtnDefaultCount){
        width = _myViewFrame.size.width / _viewCount;
    }
    
    _slideIndicatorView = [[UIView alloc] initWithFrame:CGRectMake(0, _topHeight - kSlideIndicatorViewHeight, width, kSlideIndicatorViewHeight)];
    [_slideIndicatorView setBackgroundColor:[UIColor redColor]];
    [_topMainScrollView addSubview:_slideIndicatorView];
}

#pragma mark scrollView的代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int currentPage = _scrollView.contentOffset.x / _myViewFrame.size.width;
    if ([scrollView isEqual:_scrollView]) {
        
        if ([_delegate respondsToSelector:@selector(lnSlideView:currentPage:)]) {
            [_delegate lnSlideView:self currentPage:currentPage];
        }
        
        [self updateTopMainScorllViewPosition:currentPage];
    }
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if ([_scrollView isEqual:scrollView]) {
        CGRect frame = _slideIndicatorView.frame;
        
        if (_viewCount <= kBtnDefaultCount) {
            frame.origin.x = scrollView.contentOffset.x / _viewCount;
        } else {
            frame.origin.x = scrollView.contentOffset.x / kBtnDefaultCount;
            
        }
        
        _slideIndicatorView.frame = frame;
    }
}

#pragma mark 调整TopMainScorllView的位置 不挡住按钮
- (void)updateTopMainScorllViewPosition:(int)currentPage
{
    int num = 2; // 在超过第几个按钮时自动调整scorllview
    CGFloat width = _slideIndicatorView.frame.size.width;
    
    if (currentPage > num && currentPage < _viewCount - 1) {
        [_topMainScrollView setContentOffset:CGPointMake((currentPage - num) * width, 0) animated:YES];
    } else if (currentPage <= num){
        [_topMainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    } else if ((currentPage + 1) == _viewCount) {
        [_topMainScrollView setContentOffset:CGPointMake(((currentPage + 1) - kBtnDefaultCount) * width, 0) animated:YES];
    }
}


- (void)setSlideIndicatorViewColor:(UIColor *)color
{
    _slideIndicatorView.backgroundColor = color;
}

- (void)setBtnTitleColor:(UIColor *)color
{
    for (UIButton *btn in _topBtns) {
        [btn setTitleColor:color forState:UIControlStateNormal];
    }
    [_topMainScrollView reloadInputViews];
}
@end
